/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author leandro
 */
public class TablaPeriodicaAction extends TablaPeriodicaBaseAction {
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
         try
        {
            TablaPeriodicaActionForm actionForm = (TablaPeriodicaActionForm) form;

            actionForm.setStatus("");

            if(actionForm.getNumeroAtomico() > 0) {
                this.getTablaPeriodicaSessionBean().EliminarElemento(actionForm.getNumeroAtomico());
                actionForm.setStatus("Elemento " + actionForm.getNumeroAtomico() + " eliminado.");

                actionForm.setElementos(this.getTablaPeriodicaSessionBean().ObtenerElementos());
            }

            if(actionForm.getElementos().size() <= 0)
                actionForm.setStatus("No results. " + actionForm.getStatus());

            //actionForm.setElementos(this.getTablaPeriodicaSessionBean().ObtenerElementos());
            return mapping.findForward(SUCCESS);
        }
        catch (Exception exception)
        {
            ActionErrors errors = new ActionErrors();

            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.exception", exception.getMessage()));

            saveErrors(request, errors);
            return mapping.findForward(ERROR);
        }
    }
}
